package com.gcxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.Vo.RanchVo;

import com.gcxy.dto.RanchDTO;
import com.gcxy.entity.AccountInfo;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.RanchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.crypto.interfaces.PBEKey;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiang
 * @since 2023-12-12
 */
@Service
public class RanchServiceImpl extends ServiceImpl<RanchMapper, Ranch> implements RanchService {



    public final  static  String   RANCH_ID = "RID";


    /**
     * 添加牧场
     * @param ranch
     * @return
     */
    @Override
    public boolean addRanch(Ranch ranch) {
        return this.baseMapper.insert(ranch) > 0;
    }

    /**
     * 牧场分页查询
     * @param ranchVo
     * @return
     */
    @Override
    public Page<RanchVo> getRanchByPage(RanchVo ranchVo) {
        LambdaQueryWrapper<Ranch> lqw = new LambdaQueryWrapper<>();
        //查询条件
        lqw.like(null != ranchVo.getRanchName(), Ranch::getRanchName, ranchVo.getRanchName());
        //分页对象
        Page<Ranch> ranchVoPage = new Page<>(ranchVo.getPageNumber(), ranchVo.getPageSize());
        //查询

        Page<Ranch> ranchPage = this.baseMapper.selectPage(ranchVoPage, lqw);

        Page<RanchVo> ranchVoPage1 = new Page<>();
        BeanUtils.copyProperties(ranchPage, ranchVoPage1);

        return ranchVoPage1;
    }

    /**
     * 根据id查询牧场
     * @param ranch
     * @return
     */
    @Override
    public boolean getRanchName(Ranch ranch) {
        return ObjectUtils.isEmpty(this.baseMapper.selectById(ranch.getRanchId()));
    }


    /**
     * 修改牧场信息
     * @param ranch
     * @return
     */
    @Override
    public boolean updateRanch(Ranch ranch) {
        boolean result = ObjectUtils.isEmpty(this.baseMapper.update(ranch, null));
        if(!result){
            return true;
        }
        return false ;
    }


    /**
     * 修改牧场状态
     * @param ranch
     * @return
     */
    @Override
    public boolean updateIsEnable(Ranch ranch) {
        Ranch ranch1 = this.baseMapper.selectById(ranch.getRanchId());
        if(ranch1.getIsEnable() ==0){
            ranch1.setIsEnable(1);
        }else{
            ranch1.setIsEnable(0);
        }
        return this.baseMapper.updateById(ranch1)> 0 ;
    }


    /**
     * 获取牧场名称列表
     * @return
     */
    @Override
    public List<String> getRanchNameList() {
        LambdaQueryWrapper<Ranch> lqw = new LambdaQueryWrapper<>();
        lqw.select(Ranch::getRanchName);

        return this.baseMapper.selectList(lqw).stream().map(Ranch::getRanchName).collect(Collectors.toList());
    }


    /**
     * 拆分地址（不用）
     * @param address
     * @return
     */
    public static String[] splitAddress(String address) {
        // 定义正则表达式，用于匹配省、市、区/县
        String regex = "([^\u4e00-\u9fa5]*?省|.*?市|.*?区|.*?县)([^\u4e00-\u9fa5]*?市|.*?区|.*?县)([^\u4e00-\u9fa5]*?区|.*?县)?";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(address);

        // 存储拆分结果
        String[] result = new String[3];

        // 查找匹配的部分
        if (matcher.find()) {
            for (int i = 1; i <= 3; i++) {
                // 获取匹配的组
                result[i - 1] = matcher.group(i);
                // 去除前后空格
                result[i - 1] = result[i - 1].trim();
            }
        }

        return result;
    }


}
